//
//  HorizontalAnimationItemsScrollView.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/4/15.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "HorizontalAnimationItemsView.h"
#import "Masonry.h"

@interface HorizontalAnimationItemsView()
@property (nonatomic, strong) UIScrollView *itemsScrollView;
@property (nonatomic, strong) UILabel *placeHolderLabel;
@property (nonatomic, strong) NSMutableArray *unShowItemsArray;
@property (nonatomic, strong) NSMutableArray *showItemsArray;
@end

@implementation HorizontalAnimationItemsView

#pragma mark - Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUpConstraints];
        self.layer.masksToBounds = YES;
    }
    return self;
}

#pragma mark - Private
- (void)setUpConstraints{
    self.itemsScrollView.backgroundColor = [UIColor colorWithRed:0.9255 green:0.9255 blue:0.9255 alpha:1.0];
    [self.itemsScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.width.height.equalTo(self);
    }];
    [self.placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(self.itemsScrollView);
        make.left.equalTo(self.itemsScrollView).offset(TenOffsetViewMargin);
    }];
}

- (void)resetItemsConstraints{
    _unShowItemsArray = [NSMutableArray array];
    _showItemsArray = [NSMutableArray array];
    for (UIView *subView in self.itemsScrollView.subviews) {
        if (subView.tag != 0) {
            [subView removeFromSuperview];
        }
    }
    
    for (int i=0; i<_itemsViewArray.count; i++) {
        UIView *view = [_itemsViewArray objectAtIndex:i];
        view.layer.masksToBounds = YES;
        view.tag = i+1;
        [_unShowItemsArray addObject:view];
        [self.itemsScrollView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.itemsScrollView);
            make.height.equalTo(self.itemsScrollView).multipliedBy(0.5);
            make.left.equalTo(self.mas_right);
        }];
    }
    self.placeHolderLabel.alpha = 1.0;
}

#pragma mark - Public
- (void)addItemWithItemView:(UIView *)itemView{
    if ([_showItemsArray containsObject:itemView]) {
        return;
    }
    NSInteger index = _showItemsArray.count;
    UIView *showView = [self.itemsViewArray objectAtIndex:[self.itemsViewArray indexOfObject:itemView]];
    if ([_unShowItemsArray containsObject:showView]) {
        if (index == 0) {
            [showView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.itemsScrollView).offset(HorizontalAnimationItemsViewItemsMargin);
                make.centerY.equalTo(self.itemsScrollView);
                make.height.equalTo(self.itemsScrollView).multipliedBy(0.5);
            }];
        }else{
            UIView *lastShowView = [self.showItemsArray objectAtIndex:index-1];
            [showView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastShowView.mas_right).offset(HorizontalAnimationItemsViewItemsMargin);
                make.centerY.equalTo(self.itemsScrollView);
                make.height.equalTo(self.itemsScrollView).multipliedBy(0.5);
            }];
        }
        [self.itemsScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(showView);
            make.left.top.width.equalTo(self);
            make.height.equalTo(self);
        }];
        
        [UIView animateWithDuration:HorizontalAnimationItemsViewAnimationTime animations:^{
            [self layoutIfNeeded];
        }];
        CGFloat viewWidth = self.frame.size.width;
        DDLogWarn(@"位置：%f",showView.center.x);
        CGFloat contentOffsetX = showView.center.x-viewWidth/2;
        CGFloat maxOffset = self.itemsScrollView.contentSize.width - viewWidth;
        if (contentOffsetX > maxOffset) {
            contentOffsetX = maxOffset;
        }
        contentOffsetX = contentOffsetX < 0?0:contentOffsetX;
        [self.itemsScrollView setContentOffset:CGPointMake(contentOffsetX, 0) animated:YES];
        [_unShowItemsArray removeObject:showView];
        [_showItemsArray addObject:showView];
        
        if (_showItemsArray.count>0) {
            self.placeHolderLabel.alpha = 0;
        }
    }
}

- (void)removeItemAtIndex:(NSInteger)index{
    UIView *hiddenView = [self.itemsViewArray objectAtIndex:index];
    if ([_unShowItemsArray containsObject:hiddenView]) {
        return;
    }
    [hiddenView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0);
    }];
    [UIView animateWithDuration:HorizontalAnimationItemsViewAnimationTime animations:^{
        [self layoutIfNeeded];
    }completion:^(BOOL finished) {
        NSInteger showItemsIndex = [_showItemsArray indexOfObject:hiddenView];
        UIView *nextShowView;
        if (_showItemsArray.count != 1) {
            if (showItemsIndex == 0) {
                nextShowView = [_showItemsArray objectAtIndex:showItemsIndex+1];
                [nextShowView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.itemsScrollView).offset(HorizontalAnimationItemsViewItemsMargin);
                    make.centerY.equalTo(self.itemsScrollView);
                    make.height.equalTo(self.itemsScrollView).multipliedBy(0.5);
                }];
            }else if (showItemsIndex != _showItemsArray.count-1){
                UIView *lastShowView = [_showItemsArray objectAtIndex:showItemsIndex-1];
                nextShowView = [_showItemsArray objectAtIndex:showItemsIndex+1];
                [nextShowView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastShowView.mas_right).offset(HorizontalAnimationItemsViewItemsMargin);
                    make.centerY.equalTo(self.itemsScrollView);
                    make.height.equalTo(self.itemsScrollView).multipliedBy(0.5);
                }];
            }
        }
        [hiddenView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.itemsScrollView);
            make.height.equalTo(self.itemsScrollView).multipliedBy(0.5);
            make.left.equalTo(self.mas_right);
        }];
        
        [_unShowItemsArray addObject:hiddenView];
        [_showItemsArray removeObject:hiddenView];
        if (_showItemsArray.count == 0) {
            self.placeHolderLabel.alpha = 1.0;
        }
    }];
    
}


#pragma mark - Getter
- (UIScrollView *)itemsScrollView{
    
    if (_itemsScrollView == nil) {
        _itemsScrollView = [UIScrollView new];
        _itemsScrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_itemsScrollView];
    }
    return _itemsScrollView;
}

- (UILabel *)placeHolderLabel{
    
    if (_placeHolderLabel == nil) {
        _placeHolderLabel = [UILabel new];
        _placeHolderLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
        _placeHolderLabel.textColor = IndicatorColor52;
        [self.itemsScrollView addSubview:_placeHolderLabel];
        [self.itemsScrollView sendSubviewToBack:_placeHolderLabel];
    }
    return _placeHolderLabel;
}

#pragma mark - Setter
- (void)setPlaceHolderText:(NSString *)placeHolderText{
    _placeHolderText = placeHolderText;
    self.placeHolderLabel.text = [NSString stringWithFormat:@"%@",placeHolderText];
}

- (void)setItemsViewArray:(NSMutableArray *)itemsViewArray{
    _itemsViewArray = itemsViewArray;
    [self resetItemsConstraints];
}

@end
